﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using Ymatou.MonitorHelper;

namespace YmtAuth.Domain.Model.BlacklistUser
{
    public static class YmatouBlacklistUserSpecifications
    {
        public static IMongoQuery MatchUser(string loginId)
        {
            var _loginId = loginId.ToLower();
            return Query<YmatouBlacklistUser>.EQ(e => e.Id, _loginId);
        }

        public static IMongoQuery MatchAllowLoginUser(string loginId)
        {
            var _loginId = loginId.ToLower();
            return Query.And(Query<YmatouBlacklistUser>.LT(e => e.LockdTime, DateTime.Now), Query<YmatouBlacklistUser>.EQ(e => e.Id, _loginId));
        }
        public static IMongoQuery MathRequestIP(string ip)
        {
            return Query<BlackList>.EQ(e => e.Id, ip);
        }

        public static IMongoQuery MatchDeviceId(string deviceId)
        {
      
            return Query.EQ("_id", deviceId);
      
        }

        public static IMongoUpdate IpUpdate(TimeSpan exTime)
        {
            var _exTime = DateTime.Now.Add(exTime);
            return Update.Combine(Update<BlackList>.Set(i => i.LastUpdateTime, DateTime.Now)
                , Update<BlackList>.Set(i => i.ExpiredTime, _exTime));
        }
        public static IMongoQuery MatchIpListIN(string[] ip)
        {
            return Query.In("_id", new BsonArray(ip));
        }

        /// <summary>
        /// 获取黑名单用户数据库名
        /// </summary>
        /// <returns></returns>
        public static string GetBlacklistDbName()
        {
            return "Ymt_BlacklistUser_201410";
        }

        public static string GetBlacklistTableName(string source)
        {
            var table = "YmatouBlacklist_Ip";
            if (IsSellerSource(source))
                table = "YmatouBlacklist_Ip_Seller";
            return table;
        }

        public static string GetBlacklistTableName()
        {
            return "YmatouBlacklist_Ip_Seller";
        }

        /// <summary>
        /// 返回登录被风控的提示消息
        /// </summary>
        /// <returns></returns>
        public static string GetLogin_NeedIdentityVerifyMessage(string source)
        {       
            if (IsSellerSource(source))
                return ConfigurationManager.AppSettings["SellerLoginAuthSafeTips"] ?? "由于安全原因登录失败";
            return ConfigurationManager.AppSettings["BuyerLoginAuthSafeTips"] ?? "请执行身份验证";
        }

        public static readonly string Seller_Type = "seller";
        public static readonly string Buyer_Type = "buyer";
        /// <summary>
        /// 获取IP计数器表
        /// </summary>
        /// <param name="source">用户来源或者用户类型</param>
        /// <returns></returns>
        public static string GetLoginIpCounInfoTableName(string source)
        {
            var table = "LoginIpCountInfo";
            if (IsSellerSource(source))
                table = "LoginIpCountInfo_Seller";
            return table;
        }
        /// <summary>
        /// 获取IP计数器明细
        /// </summary>
        /// <param name="source">用户来源或者用户类型</param>
        /// <returns></returns>
        public static string GetLoginIpCountListTableName(string source)
        {
            var table = "LoginIpCountListInfo";
            if (IsSellerSource(source))
                table = "LoginIpCountListInfo_Seller";
            return table;
        }

        public static bool IsSellerSource(string source)
        {
            if (source.IsEmpty()) return false;
            var sellerSource = (ConfigurationManager.AppSettings["SellerSource"] ??
                                 "seller.app.ymatou.com,seller.ymatou.com,seller").Split(new char[] { ',' });

            source = source.ToLower();
            return sellerSource.Any(c => c == source);
        }

        public static bool IsSellerOnlineSource(string source)
        {
            if (source.IsEmpty()) return false;
            var sellerSource = (ConfigurationManager.AppSettings["SellerOnlineSource"] ?? "seller.app.ymatou.com,sloginapp.ymatou.com").Split(new char[] { ',' });

            source = source.ToLower();
            return sellerSource.Any(c => c == source);
        }
    }
}
